import {getAction, deleteAction, putAction, postAction, httpAction, getJSON} from '@/api/manage'
import Vue from 'vue'
import {DEFAULT_PAGE_CONFIG, UI_CACHE_DB_DICT_DATA} from "@/store/mutation-types"
import {getInfo} from "./login";
//角色管理
const addRole = (params) => postAction( window._CONFIG['domianURL'] + "/cnRoleController/saveOrUpdateEntity", params);
const editRole = (params) => postAction( window._CONFIG['domianURL'] + "/cnRoleController/saveOrUpdateEntity", params);
const checkRoleCode = (params) => getAction( window._CONFIG['domianURL'] + "/sys/role/checkRoleCode", params);
const queryall = (params) => getAction( window._CONFIG['domianURL'] + "/sys/role/queryall", params);

//回收员管理
const auditRecycler = (params) => postAction( window._CONFIG['domianURL'] +"/recycler/approve", params);
const deleteRecycler = (params) => postAction( window._CONFIG['domianURL'] +"/recycler/deleteById", params);
const disableRecycler = (params) => postAction( window._CONFIG['domianURL'] +"/recycler/disable", params);
const enableRecycler = (params) => postAction( window._CONFIG['domianURL'] +"/recycler/enable", params);
//用户管理
const addUser = (params) => postAction( window._CONFIG['domianURL'] + "/cnUserController/saveOrUpdateCnUser", params);
const editUser = (params) => postAction( window._CONFIG['domianURL'] + "/cnUserController/saveOrUpdateCnUser", params);
const queryUserRole = (params) => postAction( window._CONFIG['domianURL'] + "/cnRoleController/getRoleList", params);
const getUserList = (params) => postAction( window._CONFIG['domianURL'] + "/cnUserController/getPageList", params);
const getUserFcList = (params) => postAction( window._CONFIG['domianURL'] + "/cnUserController/getPageListFac", params);
const frozenBatch = (params) => getAction( window._CONFIG['domianURL'] + "/cnUserController/startOrStop", params);
//验证用户是否存在
const checkOnlyUser = (params) => getAction( window._CONFIG['domianURL'] + "/sys/user/checkOnlyUser", params);
//改变密码
const changePassword = (params) => postAction( window._CONFIG['domianURL'] + "/cnUserController/changePassword", params);

//权限管理
const addMenu = params => postAction( window._CONFIG['domianURL'] + '/cnFunctionController/saveOrUpdateEntity',params)
const addPermission = (params) => postAction( window._CONFIG['domianURL'] + "/sys/permission/add", params);
const editPermission = (params) => putAction( window._CONFIG['domianURL'] + "/sys/permission/edit", params);
const getPermissionList = (params) => postAction( window._CONFIG['domianURL'] + "/cnFunctionController/findTreeList", params);
/*update_begin author:wuxianquan date:20190908 for:添加查询一级菜单和子菜单查询api */
const getSystemMenuList = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/getSystemMenuList", params);
const getSystemSubmenu = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/getSystemSubmenu", params);
const getSystemSubmenuBatch = (params) => getAction( window._CONFIG['domianURL'] + '/sys/permission/getSystemSubmenuBatch', params)

const queryTreeList = (params) => getAction( window._CONFIG['domianURL'] + "/cnFunctionController/findTreeSelectList", params);
const queryTreeListForRole = (params) => getAction( window._CONFIG['domianURL'] + "/cnFunctionController/findTreeSelectList", params);
const queryTreeListForTenant = (params) => getAction( window._CONFIG['domianURL'] + "/sys/manage/findTreeListByUser", params);
const queryListAsync = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/queryListAsync", params);
const queryRolePermission = (params) => getAction( window._CONFIG['domianURL'] + "/cnFunctionController/getFunListByRole", params);
const queryTenantRolePermission = (params) => postAction( window._CONFIG['domianURL'] + "/sys/manage/findTreeList", params);
const saveRolePermission = (params) => postAction(window._CONFIG['domianURL'] +"/cnRoleFunController/saveOrUpdateEntity", params);
const saveTenantPermission = (params) => postAction(window._CONFIG['domianURL'] + "/sys/manage/initSys", params);
const openTenantAccount = (params) => getAction(window._CONFIG['domianURL'] + "/sys/manage/oepn", params);

//获取路由菜单
const queryPermissionsByUser = async (params) => {
    return Promise.all([getAction( window._CONFIG['domianURL'] + "/cnFunctionController/findTreeListByUser", params),await getInfo()])
}

// const queryPermissionsByUser = (params)=>getAction(`http://localhost:3000/data/isystem/menu.json`,params);
const loadAllRoleIds = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/loadAllRoleIds", params);
const getPermissionRuleList = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/getPermRuleListByPermId", params);
const queryPermissionRule = (params) => getAction( window._CONFIG['domianURL'] + "/sys/permission/queryPermissionRule", params);

// 部门管理
const queryDepartTreeList = (params)=>postAction( window._CONFIG['domianURL'] + "/cnOrgController/findTreeListNew",params);
// const queryDepartTreeList = () => getJSON(location.pathname);
const queryIdTree = (params) => getAction( window._CONFIG['domianURL'] + "/cnOrgController/getCnOrgByIdOrOrgCode", params);
const queryParentName = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysDepart/queryParentName", params);
const searchByKeywords = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysDepart/searchBy", params);
const deleteByDepartId = (params) => getAction( window._CONFIG['domianURL'] + "/cnOrgController/deleteAllByidOrCode", params);

//二级部门管理
const queryDepartPermission = (params) => getAction( window._CONFIG['domianURL'] + "/cnOrgController/getCnOrgByIdOrOrgCode", params);
const saveDepartPermission = (params) => postAction( window._CONFIG['domianURL'] + "/sys/permission/saveDepartPermission", params);
const queryTreeListForDeptRole = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysDepartPermission/queryTreeListForDeptRole", params);
const queryDeptRolePermission = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysDepartPermission/queryDeptRolePermission", params);
const saveDeptRolePermission = (params) => postAction( window._CONFIG['domianURL'] + "/sys/sysDepartPermission/saveDeptRolePermission", params);
const queryMyDepartTreeList = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysDepart/queryMyDeptTreeList", params);

//日志管理
//const getLogList = (params)=>getAction( window._CONFIG['domianURL'] + "/sys/log/list",params);
const deleteLog = (params) => deleteAction( window._CONFIG['domianURL'] + "/sys/log/delete", params);
const deleteLogList = (params) => deleteAction( window._CONFIG['domianURL'] + "/sys/log/deleteBatch", params);

//数据字典
const addDict = (params) => postAction(window._CONFIG['domianURL'] + "/cnDictionaryTypeController/saveOrUpdateEntity", params);
const editDict = (params) => postAction(window._CONFIG['domianURL'] + "/cnDictionaryTypeController/saveOrUpdateEntity", params);
const treeList = (params) => getAction( window._CONFIG['domianURL'] + "/sys/dict/treeList", params);
const addDictItem = (params) => postAction( window._CONFIG['domianURL'] + "/cnDictionaryController/saveOrUpdateEntity", params);
const editDictItem = (params) => postAction( window._CONFIG['domianURL'] + "/cnDictionaryController/saveOrUpdateEntity", params);

//字典标签专用（通过code获取字典数组）
export const ajaxGetDictItems =(dictCode,params) =>{
  return new Promise(async (resolve, reject)=>{
    getAction(window._CONFIG['domianURL'] + `/cnDictionaryController/getDictByCode`, {dictCode}).then(res=>{
      if (!Vue.ls.get(UI_CACHE_DB_DICT_DATA) || !Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
        let dict = Vue.ls.get(UI_CACHE_DB_DICT_DATA)||{};
        dict[dictCode] = res.result
        Vue.ls.set(UI_CACHE_DB_DICT_DATA,dict,)
      }
      resolve(res)
    }).catch(err=>{
      reject(err)
    })
  })
}

//从缓存中获取字典配置
function getDictItemsFromCache(dictCode) {
    if (Vue.ls.get(UI_CACHE_DB_DICT_DATA) && Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode]) {
        let dictItems = Vue.ls.get(UI_CACHE_DB_DICT_DATA)[dictCode];
        console.log("-----------getDictItemsFromCache----------dictCode=" + dictCode + "---- dictItems=", dictItems)
        return dictItems;
    }
}

//获取页面配置信息
function getPageConfig(){
    var  url = window._CONFIG['domianURL']
    var reg = /^http(s)?:\/\/(.*?)\//
    // 把host替换成指定数值
    var ToReplace = 'Host/'
    url.replace(reg, ToReplace)
    // Host/cench
    //******************************************
    return postAction( window._CONFIG['domianURL'] + '/sys/config/getSysConfig',{
      // "url": 'recover.chaona.top'
      "url": reg.exec(url)[2]
    })
}

//系统通告
const doReleaseData = (params) => getAction( window._CONFIG['domianURL'] + "/sys/annountCement/doReleaseData", params);
const doReovkeData = (params) => getAction( window._CONFIG['domianURL'] + "/sys/annountCement/doReovkeData", params);
//获取系统访问量
const getLoginfo = (params) => getAction( window._CONFIG['domianURL'] + "/sys/loginfo", params);
const getVisitInfo = (params) => getAction( window._CONFIG['domianURL'] + "/sys/visitInfo", params);

// 根据部门主键查询用户信息
const queryUserByDepId = (params) => getAction( window._CONFIG['domianURL'] + "/cnUserController/queryUserByDepId", params);

// 重复校验
const duplicateCheck = (params) => postAction( window._CONFIG['domianURL'] + "/cnButtonController/duplicate/check", params);
// 加载分类字典
const loadCategoryData = (params) => getAction( window._CONFIG['domianURL'] + "/sys/category/loadAllData", params);
const checkRuleByCode = (params) => getAction( window._CONFIG['domianURL'] + '/sys/checkRule/checkByCode', params)
//加载我的通告信息
const getUserNoticeInfo = (params) => getAction( window._CONFIG['domianURL'] + "/sys/sysAnnouncementSend/getMyAnnouncementSend", params);
const getTransitURL = url => `/sys/common/transitRESTful?url=${encodeURIComponent(url)}`

//商品分类
const getProductCategory = params => postAction(window._CONFIG['domianURL'] + '/goodsclassify/cnGoodsClassify/getProductTypeTree',params)



// 中转HTTP请求
export const transitRESTful = {
    get: (url, parameter) => getAction(getTransitURL(url), parameter),
    post: (url, parameter) => postAction(getTransitURL(url), parameter),
    put: (url, parameter) => putAction(getTransitURL(url), parameter),
    http: (url, parameter) => httpAction(getTransitURL(url), parameter),
}

export {
    getProductCategory,
    addMenu,
    addRole,
    editRole,
    checkRoleCode,
    addUser,
    editUser,
    queryUserRole,
    getUserList,
    getUserFcList,
    queryall,
    frozenBatch,
    checkOnlyUser,
    changePassword,
    getPermissionList,
    addPermission,
    editPermission,
    queryTreeList,
    queryListAsync,
    queryRolePermission,
    saveRolePermission,
    queryPermissionsByUser,
    loadAllRoleIds,
    getPermissionRuleList,
    queryPermissionRule,
    queryDepartTreeList,
    queryIdTree,
    queryParentName,
    searchByKeywords,
    deleteByDepartId,
    deleteLog,
    deleteLogList,
    addDict,
    editDict,
    treeList,
    addDictItem,
    editDictItem,
    doReleaseData,
    doReovkeData,
    getLoginfo,
    getVisitInfo,
    queryUserByDepId,
    duplicateCheck,
    queryTreeListForRole,
    getSystemMenuList,
    getSystemSubmenu,
    getSystemSubmenuBatch,
    loadCategoryData,
    checkRuleByCode,
    queryDepartPermission,
    saveDepartPermission,
    queryTreeListForDeptRole,
    queryDeptRolePermission,
    saveDeptRolePermission,
    queryMyDepartTreeList,
    getUserNoticeInfo,
    getDictItemsFromCache,
    getPageConfig,
    queryTenantRolePermission,
    saveTenantPermission,
    queryTreeListForTenant,
    openTenantAccount,
    auditRecycler,
    deleteRecycler,
    disableRecycler,
    enableRecycler
}



